/*
Copyright 2010 Johan Maasing

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package nu.zoom.android.gdata.project.client;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author Johan Maasing
 * 
 */
public class Paginator extends LinearLayout {
	private TextView pageView;
	private Button nextButton;
	private Button previousButton;

	/**
	 * @param context
	 */
	public Paginator(final Context context) {
		super(context);
		init(context);
	}

	public Paginator(final Context context, final AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	private void init(final Context context) {
		LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.paginator, this, true);
		pageView = (TextView) findViewById(R.id.PaginatorPageNumberTextView);
		previousButton = (Button) findViewById(R.id.PaginatorButtonPrevious);
		nextButton = (Button) findViewById(R.id.PaginatorButtonNext);
	}

	public void setPreviousListener(View.OnClickListener l) {
		previousButton.setOnClickListener(l);
	}

	public void setNextListener(View.OnClickListener l) {
		nextButton.setOnClickListener(l);
	}

	public void setIndices(final int start, final int end, final int total) {
		if (total > (end - start)+1) {
			setVisibility(View.VISIBLE);
		} else {
			setVisibility(View.GONE);
		}
		nextButton.setEnabled(total > end);
		previousButton.setEnabled(start > 1);
		pageView.setText(start + " - " + end + " / " + total);
	}

}
